<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\ActivityPhoto;
use app\common\tool\Result;
use think\facade\View;
use think\response\Json;
use app\common\model\Mechanism as MechanismModel;
use \app\common\model\User;
use think\exception\ValidateException;

class Mechanism extends Base
{


    public function getList(): \think\Response
    {
        $list = MechanismModel::where(['status'=>2,'is_del'=>0])->field('id,title')->select();
        return Result::success('获取成功',$list);
    }


    public function save(): Json
    {
        $param = $this->request->param();
        try {
            $this->validate($param, 'Mechanism');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        return json(MechanismModel::add($param, $this->user));
    }


    //申领情况
    public function receive_list(): \think\Response
    {
        $param = $this->request->param();
        $where['is_del'] = 0;
        $where['mechanism_uid'] = $this->user->id;
        $where['is_receive'] = $param['type'];
        $list = User::where($where)->field('id,nickname,mobile,photo')->select();
        return Result::success('获取成功',$list);
    }


    //上传情况
    public function upload_list(): \think\Response
    {
        $param = $this->request->param();
        $activity = \app\common\model\Activity::find($param['aid']);
        $pages = $activity->pages;
        $where['is_del'] = 0;
        $where['mechanism_uid'] = $this->user->id;
//        $list = User::where($where)->field('id,nickname,photo')->select();
        $user_ids = User::where($where)->column('id');
        $u_ids = ActivityPhoto::whereIn('u_id',$user_ids)->where('a_id',$param['aid'])->group('u_id')->column('u_id');
        $list = User::whereIn('id',$u_ids)->field('id,nickname,photo')->select();
        foreach ($list as $k=>$v)
        {
            $num = ActivityPhoto::where(['u_id'=>$v->id])->count();
            $v->schedule = $num/$pages*100;

        }
        return Result::success('获取成功',$list);
    }
}

